<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <#include "/pubHeadStyleTree.html">
</head>
<body>
<link rel="stylesheet" href="${ctx}/extends/formSelects/formSelects-v4.css">
<script src="${ctx}/extends/formSelects/formSelects-v4.js" type="text/javascript"></script>
<div style="height: 100%;padding: 10px 10px 50px;overflow: hidden;box-sizing: border-box;">
    <form class="layui-form" action="" style="height: 100%;overflow-y: auto;">
        <table class="layui-table" id="nodeSetTable">
            <thead>
            <tr>
                <th>节点名称</th>
                <th>用户</th>
                <th>角色</th>
            </tr>
            </thead>
            <tbody class="tbody" id="tbodyUserSet">
            </tbody>
        </table>
    </form>
    <div class="layui-btn-container" style="text-align:center;position:absolute;left:0px;right: 0px;bottom: 10px;">
        <button class="layui-btn" lay-submit id="save">保存</button>
        <button class="layui-btn layui-btn-danger" id="return">取消</button>
    </div>
</div>
<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','form','setter','element', 'commons'],function(){
        var setter=layui.setter
            ,element = layui.element
            ,form = layui.form
            ,$ = layui.$
            ,commons = layui.commons;
        var formSelects = layui.formSelects;
        var modelId = '${modelId}';
        var procDefId = "";
        var parentProcDefId = "${parentProcDefId}";
        $.ajax({
            url: "${ctx}/workflow/actDefModel/userSetQuickly",
            type: "get",
            contentType: 'application/json',
            dataType: 'json',
            data:{"modelId":modelId, "parentProcDefId":parentProcDefId},
            success: function (result) {
                procDefId = result.data.actDefModel.procDefId;
                var nodeSetList = result.data.nodeSetList;
                var userlist = result.data.userList;
                var rolelist = result.data.roleList;

                //用户下拉列表
                var commonUserOption = "";
                for(var i=0;i<userlist.length;i++){
                    var orgName = userlist[i].orgName;
                    var users = userlist[i].userList;
                  //  commonUserOption+="<optgroup label=\""+orgName+"\">";
                    for(var j=0;j<users.length;j++){
                        commonUserOption+="<option value=\""+users[j].userId+"\">"+users[j].fullName+"["+orgName+"]</option>";
                    }
                   // commonUserOption+="</optgroup>";
                }

                //角色下拉列表
                var commonRoleOption = "";
                for(var i=0;i<rolelist.length;i++){
                    var role = rolelist[i];
                    commonRoleOption+="<option value=\""+role.roleId+"\">"+role.roleName+"</option>";
                }

                for(var i=0;i<nodeSetList.length;i++){
                    var nodeSet = nodeSetList[i];
                    var nodeName = nodeSet.nodeName;
                    var nodeId = nodeSet.nodeId;
                    var setId = nodeSet.setId;
                    var td = "<td>"+nodeName+"("+nodeId+")<input type=\"hidden\" name=\"nodeId\" value=\""+nodeId+"\"><input type=\"hidden\" name=\"setId\" value=\""+setId+"\"></td>"
                    var commonUserTd = "<td><select lay-search xm-select='user"+setId+"'>" +commonUserOption+"</select></td>";
                    var commonRoleTd = "<td><select lay-search xm-select='role"+setId+"'>"+commonRoleOption+"</select></td>";
                    $('.tbody').append("<tr>"+td+commonUserTd+commonRoleTd+"</tr>");
                }
                form.render();
                if(formSelects!=undefined){
                    formSelects.render();
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                layer.alert("暂无数据，请稍后操作！" + XMLHttpRequest.status);
            }
        });

        var thisIframeIndex = window.parent.layer.getFrameIndex(window.name);
        $('#return').click(function () {
            window.parent.layer.close(thisIframeIndex);
        });

        $('#save').click(function () {
            var param={
                modelId:modelId,
                procDefId:procDefId
            };
            var userSetting = extractNodeUserDatas();
            if(!userSetting){
                layer.alert("没有设置用户!");
                return false;
            }
            param.userSetting = userSetting;
            if(parentProcDefId){
                param.parentProcDefId = parentProcDefId;
            }

            $.ajax({
                url: "${ctx}/workflow/bpmUserCondition/saveQuickly",
                type: "POST",
                data: param,
                dataType:"json",
                success: function (data) {
                    if (commons._filterRecord(data)) {
                        window.parent.layer.alert(data.msg, function (index) {
                            //关闭弹出框
                            window.parent.layer.close(index);
                            //关闭iframe框
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        });
                    } else {
                        window.parent.layer.alert(data.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    window.parent.layer.alert("保存失败！" + XMLHttpRequest.status);
                }
            });
        });

        var assignTypeUsers = "users";
        var assignTypeUsersName = "用户";
        var assignTypeRole = "role";
        var assignTypeRoleName = "角色";

        // 获取用户设置
        function extractNodeUserDatas(){
            var userSetting = new Array();
            $("#tbodyUserSet").find("tr").each(function(){
                var users = new Array();
                var tr = $(this);
                //节点id
                var nodeId = tr.find("[name='nodeId']").val();
                //节点设置id
                var setId = tr.find("[name='setId']").val();
                var cmpIds_users;
                var cmpNames_users;
                cmpIds_users = formSelects.value('user'+setId, 'valStr');
                cmpNames_users = formSelects.value('user'+setId, 'nameStr');

                var cmpIds_role;
                var cmpNames_role;
                cmpIds_role = formSelects.value('role'+setId, 'valStr');
                cmpNames_role = formSelects.value('role'+setId, 'nameStr');

                var joinTxt = [];
                if(cmpIds_users){
                    var user={
                        assignType:assignTypeUsers,
                        nodeId:nodeId,
                        setId:setId,
                        cmpIds:cmpIds_users,
                        cmpNames:cmpNames_users,
                        compType:0,
                        extractUser:0
                    };
                    users.push(user);
                    joinTxt.push(assignTypeUsersName);
                    joinTxt.push(':');
                    joinTxt.push(cmpNames_users);
                }
                if(cmpIds_role){
                    var user={
                        assignType:assignTypeRole,
                        nodeId:nodeId,
                        setId:setId,
                        cmpIds:cmpIds_role,
                        cmpNames:cmpNames_role,
                        compType:0,
                        extractUser:0
                    };
                    users.push(user);
                    if(joinTxt.length > 0){
                        joinTxt.push(' ');
                        joinTxt.push('或');
                        joinTxt.push(' ');
                    }
                    joinTxt.push(assignTypeRoleName);
                    joinTxt.push(':');
                    joinTxt.push(cmpNames_role);
                }
                if(users.length>0){
                    var setting={
                        nodeId:nodeId,
                        setId:setId,
                        conditionShow:joinTxt.join(''),
                        users:JSON.stringify(users)
                    };
                    userSetting.push(setting);
                }
            });
            if(userSetting.length==0) return;
            return JSON.stringify(userSetting);
        };

    });
</script>
<style>
    .xm-select-parent .xm-select-title div.xm-select-label>span{
        height: auto;
    }
    .xm-select-parent .xm-form-select dl{
        bottom: auto !important;
    }
</style>
</body>
</html>

